var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var WEBPACK_ENV  = process.env.WEBPACK_ENV || 'dev';
var HtmlWebpackPlugin = require('html-webpack-plugin');
var getHtmlConfig = function(name,title){
    return{
            template : './src/view/' + name + '.html',
            filename : 'view/' + name + '.html',
            title    : title,
            inject   : true,
            hash     : true,
            chunks   : ['common',name]
    };
};
//获取HTML——webpack-plugin参数的方法
var config = {
	 entry : {
     	common : ['./src/page/common/index.js'],//公共组件这里打包一个client
     	//全局通用逻辑
     	index                 : ['./src/page/index/index.js'],
     	login                 : ['./src/page/login/index.js'],
        newQuestionnaireList  : ['./src/page/newQuestionnaireList/index.js'],
        newComplete           : ['./src/page/newComplete/index.js'],
        echarts               : ['./src/page/echarts/index.js']
     },
       //页面入口文件配置
     output: {
         path: './dist',
         publicPath : '/dist',
         filename : 'js/[name].js'
     },
      resolve : {
        alias : {
            node_modules    : __dirname + '/node_modules',
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image'
        }
    },
     /*用来引用外部的模块或变量*/
     externals : { 
     	jquery : 'window.jQuery'
     	/*引入jquery,以便在js模块化的引用jquer*/
     },
      plugins   : [
      	new webpack.optimize.CommonsChunkPlugin({
     		name     : 'common',
     		filename : 'js/base.js' 
      	}),
      	 //独立通用模块的js/base.js
        new ExtractTextPlugin("css/[name].css"),
        //把css单独打包到文件里
        new HtmlWebpackPlugin(getHtmlConfig('index','首页')),
        new HtmlWebpackPlugin(getHtmlConfig('login','用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('newQuestionnaireList','新建问卷')),
        new HtmlWebpackPlugin(getHtmlConfig('newComplete','问卷编辑页')),
        new HtmlWebpackPlugin(getHtmlConfig('echarts','数据分析页'))
      ],
      module: {
        loaders:[
                    { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader") },//单独打包的css的
                    { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=100&name=resource/[name].[ext]' },
                    { test: /\.string$/, loader: 'html-loader' }/*??????????????????*/
                ]
    }
};
if(WEBPACK_ENV === 'dev'){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8085');
}
module.exports = config;